package com.javasm.commons.exception;

import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.enums.E;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

import java.io.FileNotFoundException;

//控制器增强注解,当控制层出现异常时,进入ControllerAdvice注解的对象
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class GlobExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public AxiosResult doArithmeticException(ArithmeticException e){
        System.out.println(e.getMessage());
        return AxiosResult.error(E.NOT_EXISTS,e.getMessage());
    }

    @ExceptionHandler(FileNotFoundException.class)
    public AxiosResult doFileNotFoundException(FileNotFoundException e){
        System.out.println(e.getMessage());
        return AxiosResult.error(e.getMessage());
    }

    @ExceptionHandler(MvcException.class)
    public AxiosResult doMvcException(MvcException e){
        return AxiosResult.error(e.getE());
    }

    @ExceptionHandler(MultipartException.class)
    public AxiosResult doMultipartException(MultipartException e){
        return AxiosResult.error(E.FILE_MAX_SIZE);
    }

    @ExceptionHandler(Exception.class)
    public AxiosResult doException(Exception e){
        System.out.println(e.getMessage());
        return AxiosResult.error(e.getMessage());
    }
}
